import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { wechatIdentity } from "@/utils/wechatAuth";

const routes: Array<RouteRecordRaw> = [
    {
        path: '/dist/:customerUuid/test',
        name: 'Test',
        component: () =>import('@/views/Test.vue')
    },
    {
        path: '/dist/:customerUuid/info',
        name: 'Info',
        component: () =>import('@/views/Info.vue')
    },
    {
        path: '/dist/:customerUuid/home',
        name: 'Home',
        component: () =>import('@/views/Home.vue')
    },
    {
        path: '/dist/:customerUuid/about',
        name: 'About',
        component: () =>import("@/views/About.vue")
    },
    {
        path: "/dist/:customerUuid/leads",
        name: "leads",
        meta: { title: 'YiDigital-潜客列表' },
        component: () => import("@/views/leads/leads.vue")
    },
    {
        path: "/dist/:customerUuid/leads/:uuid",
        name: "leadsDetail",
        meta: { title: 'YiDigital-潜客详情' },
        component: () => import("@/views/leads/leadsDetail.vue")
    },
    {
        path: "/dist/:customerUuid/leadsRecord/:leadsUuid",
        name: "recordDetail",
        meta: { title: 'YiDigital-潜客日志' },
        component: () => import("@/views/leads/leadsRecord.vue")
    },
    {
        path: "/dist/:customerUuid/pool",
        name: "pool",
        meta: { title: 'YiDigital-公海池' },
        component: () => import("@/views/leads/pool.vue")
    },
    {
        path: "/dist/:customerUuid/analysis",
        name: "analysis",
        meta: { title: 'YiDigital-数据看板' },
        component: () => import("@/views/leads/analysis.vue")
    },
    {
        path: "/dist/:customerUuid/tool",
        name: "tool",
        meta: { title: 'YiDigital-更多工具' },
        component: () => import("@/views/leads/tool.vue")
    },
    {
        path: "/dist/:customerUuid/card/:saleUuid",
        name: "card",
        meta: { title: 'YiDigital-电子名片' },
        component: () => import("@/views/leads/card.vue")
    },
    {
        path: "/dist/:customerUuid/aggList/:aggListUuid",
        name: "aggList",
        meta: { title: 'YiDigital-聚合列表' },
        component: () => import("@/views/aggList/index.vue")
    },
    {
        path: "/dist/:customerUuid/content/:contentUuid/:aggListUuid?",
        name: "contentDetail",
        meta: { title: 'YiDigital-内容详情' },
        component: () => import("@/views/content/detail.vue")
    },
    {
        path: "/dist/:customerUuid/campaign/:campaignUuid/:aggListUuid?",
        name: "campaignDetail",
        meta: { title: 'YiDigital-活动详情' },
        component: () => import("@/views/campaign/detail.vue")
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});



router.beforeEach(async (to, _from, next) => {
    // 设置页面标题
    document.title = (to.meta.title as string) || '';
    // 设置 customerUUID
    const customerUuid = to.params.customerUuid as string | undefined;
    if (customerUuid) {
        localStorage.setItem('customerUUID', customerUuid);
    } else {
        localStorage.removeItem('customerUUID');
    }
    if (to.name != 'Home' && to.name != 'Test') {
        await wechatIdentity(to);
    }
    next();
});

export default router;
